package com.sharkburner.aeris.ui.module.site.component;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sharkburner.aeris.ui.module.site.model.AddSiteModel;
import com.sharkburner.common.listener.RepaintListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@Component("prototype")
public class AddSiteComponent extends VerticalLayout implements ClickListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private HorizontalLayout mainLayout;
	private TextField siteTextField;
	private Button addButton;

	@Autowired
	private AddSiteModel addSitemodel;

	private RepaintListener repaintLitener;

	@PostConstruct
	public void init() {
		buildMainLayout();
	}

	private void buildMainLayout() {
		// initialization layouts
		mainLayout = new HorizontalLayout();
		mainLayout.setSpacing(true);

		// add textField
		siteTextField = new TextField();
		siteTextField.setInputPrompt("Source. Example: 'http://address.com' ");
		siteTextField.setRequired(true);
		siteTextField.setWidth("300px");
		mainLayout.addComponent(siteTextField);

		// add Button
		addButton = new Button("Add");
		addButton.addListener(this);
		mainLayout.addComponent(addButton);

		// add mainLayout to Composition
		addComponent(mainLayout);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if (siteTextField.isValid()) {
			addSitemodel.setLayout(mainLayout);
			addSitemodel.addSource((String) siteTextField.getValue());
			if(repaintLitener!=null){
				repaintLitener.repaint();
			}
		}
	}

	public void setRepaintListener(RepaintListener repaintLitener) {
		this.repaintLitener=repaintLitener;
	}

}
